package com.xdwanj.hello;

import java.sql.*;

public class helloJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
/*
        //要连接的数据库URL
//                    jdbc:数据库协议写法//主机端口号/数据库?使用什么编码 & 字符集使用什么编码 & 确保数据库版本低于JDBC版本也能使用
//      String url = "jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
      String url = "jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf8&useSSL=true";

//        String usl = "jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf8&useSSL=true";

        //连接的数据库时使用的用户名
//        String username = "root";
        String username = "root";

        //连接的数据库时使用的密码
//        String password = "123456";
        String password = "123456";

        //1.加载驱动
//        Class.forName("com.mysql.jdbc.Driver");//推荐使用这种方式来加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取与数据库的链接
//        Connection conn = DriverManager.getConnection(url, username,password);
        Connection conn = DriverManager.getConnection(url, username, password);

        //3.获取用于向数据库发送sql语句的statement
//        Statement st = conn.createStatement();
//        String sql = "select id,name,password,email,birthday from users";
        Statement st = conn.createStatement();
        String sql = "select * from emp";

        //4.向数据库发sql,并获取代表结果集的results
//        ResultSet rs = st.executeQuery(sql);
        ResultSet rs = st.executeQuery(sql);

        //5.取出结果集的数据
//        while (rs.next()) {
//            System.out.println("id=" + rs.getObject("id"));
//            System.out.println("name=" + rs.getObject("name"));
//            System.out.println("password=" + rs.getObject("password"));
//            System.out.println("email=" + rs.getObject("email"));
//            System.out.println("birthday=" + rs.getObject("birthday"));
//        }
        while (rs.next()) {
            System.out.println(rs.getObject("empno"));
            System.out.println(rs.getObject("ename"));
            System.out.println(rs.getObject("job"));
            System.out.println(rs.getObject("mgr"));
            System.out.println(rs.getObject("hiredate"));
            System.out.println(rs.getObject("sal"));
            System.out.println(rs.getObject("comm"));
            System.out.println(rs.getObject("deptno"));
            System.out.println("=====================================================");
        }

        //6.关闭链接，释放资源
//        rs.close();
//        st.close();
//        conn.close();
        rs.close();
        st.close();
        conn.close();

 */
        String url = "jdbc:mysql://localhost:3306/scott?useUnicode=true&character=utf8&useSSL=true";
        String username = "root";
        String password = "123456";

        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection(url, username, password);

        Statement state = conn.createStatement();

        String sql = "select * from emp;";
        ResultSet resultSet = state.executeQuery(sql);

        while (resultSet.next()) {
            System.out.println(resultSet.getObject("empno"));
            System.out.println(resultSet.getObject("ename"));
            System.out.println(resultSet.getObject("job"));
            System.out.println(resultSet.getObject("mgr"));
            System.out.println(resultSet.getObject("hiredate"));
            System.out.println(resultSet.getObject("sal"));
            System.out.println(resultSet.getObject("comm"));
            System.out.println(resultSet.getObject("deptno"));
            System.out.println("=====================================================");
        }

        resultSet.close();
        state.close();
        conn.close();
    }
}
